﻿using System;
using System.Collections.Generic;
using System.Linq;
using Microsoft.AspNetCore.Mvc;
using RayD.Core;
using RayD.Repository.Entites.SystemModule;
using RayD.App.SystemModule;
using RayD.WebApi.Models;
using RayD.Repository.Core;

namespace RayD.WebApi.Controllers.Admin
{
    /// <summary>
    /// 部门信息资源
    /// </summary>
    public class DepartmentController : BaseAdminController
    {
        #region 构造函数注入
        private DepartmentApp _Department;
        public DepartmentController(DepartmentApp Department)
        {
            this._Department = Department;
        }
        #endregion

        #region 参数实体
        /// <summary>
        /// 分页数据查询响应参数
        /// </summary>
        public class GetPageRespone
        {
            /// <summary>
            /// 查询响应数据
            /// </summary>
            public IEnumerable<DepartmentEntity> rows { get; set; }
            /// <summary>
            /// 总记录数
            /// </summary>
            public int records { get; set; }
        }
        #endregion

        #region 获取数据
        /// <summary>
        /// 获取分页列表数据
        /// </summary>
        /// <param name="queryjson">查询参数</param>
        /// <param name="pagination">分页参数</param>
        /// <returns></returns>
        [HttpGet]
        public ResParameter<GetPageRespone> GetPageList(string queryjson, string pagination)
        {
            var paginationObj = pagination.ToObject<Pagination>();
            var data = _Department.GetPageList(queryjson, paginationObj);
            return Success(new GetPageRespone()
            {
                rows = data,
                records = paginationObj.records
            });
        }
        /// <summary>
        /// 获取树型列表数据
        /// </summary>
        /// <param name="B_ManaframeId">组织架构Id</param>
        /// <param name="isDeep">是否深度查询</param>
        /// <returns></returns>
        [HttpGet]
        public ResParameter<List<DepartmentEntity>> GetTreeList(string B_ManaframeId, bool isDeep)
        {
            var list = _Department.GetTreeList(AuthenticationHelper.LoginUserInfo.account, B_ManaframeId, isDeep);
            return Success(list);
        }
        /// <summary>
        /// 根据架构主键获取列表数据
        /// </summary>
        /// <param name="B_ManaframeId">组织架构Id</param>
        /// <param name="isDeep">是否深度查询</param>
        /// <returns></returns>
        [HttpGet]
        public ResParameter<List<DepartmentEntity>> GetListByManaId(string B_ManaframeId, bool isDeep)
        {
            var list = _Department.GetListByManaId(AuthenticationHelper.LoginUserInfo.account, B_ManaframeId, isDeep);
            return Success(list);
        }
        #endregion

        #region 提交数据

        /// <summary>
        /// 保存实体(新增,编辑)
        /// </summary>
        /// <param name="request">部门信息实体</param>
        /// <returns></returns>
        [HttpPut, Logging("部门数据保存")]
        public ResParameter SaveForm(DepartmentEntity request)
        {
            var (isSuccess, msg) = _Department.SaveForm(request);
            if (isSuccess)
            {
                return Success(msg);
            }
            else
            {
                return Fail(msg);
            }
        }
        /// <summary>
        /// 删除实体
        /// </summary>
        /// <param name="request">部门信息实体</param>
        /// <returns></returns>
        [HttpPut, Logging("部门数据删除")]
        public ResParameter DeleteForm(DepartmentEntity request)
        {
            var (isSuccess, msg) = _Department.DeleteForm(request);
            if (isSuccess)
            {
                return Success(msg);
            }
            else
            {
                return Fail(msg);
            }
        }
        /// <summary>
        /// 状态更改
        /// </summary>
        /// <param name="request">实体</param>
        /// <returns></returns>
        [HttpPut, Logging("部门状态更改")]
        public ResParameter ChangeState(DepartmentEntity request)
        {
            var (isSuccess, msg) = _Department.ChangeState(request);
            if (isSuccess)
            {
                return Success(msg);
            }
            else
            {
                return Fail(msg);
            }
        }

        #endregion

        #region 数据验证
        /// <summary>
        /// 验证字段值是否存在
        /// </summary>
        /// <param name="id">实体主键</param>
        /// <param name="field">字段名</param>
        /// <param name="value">字段值</param>
        /// <param name="manaId">架构主键</param>
        /// <returns></returns>
        [HttpGet]
        public ResParameter ValidateExist(string id, string field, string value, string manaId)
        {
            var res = _Department.ValidateExist(id, field, value, manaId);
            if (!res)
            {
                return Success("校验通过");
            }
            else
            {
                return Fail("值已存在，校验不通过！");
            }
        }
        #endregion
    }
}
